var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");

var AD_CONST_MIN_INTERVAL = 40;
var AD_CONST_MAX_GO_BACK_TIMES = 4;

var ad = {
  in_task: false,
  in_home: false,
  if_current_ad_is_over: null, //判断广告实际是否比预计时间提前结束
  if_in_home: null, //判断是否在首页
  if_in_task: null, //判断是否是在任务页
  has_next_contents: [], //判断是否有下一个广告
  has_watch_ad_times: 0,
  max_ad_times: 1000,
  init(
    if_current_ad_is_over,
    if_in_task,
    if_in_home,
    has_next_contents,
    max_ad_times
  ) {
    console.show();
    var logger = utils.create_logger("ad init: ");
    logger("");
    this.if_current_ad_is_over = //判断当前广告是否已经结束
      if_current_ad_is_over ||
      function () {
        var over_contents = ["已领取", "已完成任务"];
        for (var i = 0; i < over_contents.length; i++) {
          var over_content = over_contents[i];
          if (utils.exists_text_starts_with(over_content)) {
            logger("找到-" + over_content + "-");
            return true;
          }
        }
        return false;
      };
    this.if_in_task = //判断是否在开始看广告的页面
      if_in_task ||
      function () {
        var in_task_contents = ["立即收下", "开心收下", "现金收益"];
        for (var i = 0; i < in_task_contents.length; i++) {
          var in_task_content = in_task_contents[i];
          if (utils.exists_text_starts_with(in_task_content)) {
            console.log("ad init: 找到-" + in_task_content + "-");
            return true;
          }
        }
        return false;
      };
    this.if_in_home = //判断是否在app首页
      if_in_home ||
      function () {
        return ocr_utils.screen_exists_text_all(["视频", "小说"], true);
      };
    this.has_next_contents = has_next_contents || [
      //判断是否有下一个广告可以观看
      "再看一个领取",
      "再看一条领取",
      "再看1个获得",
      "换个视频",
    ];
    this.max_ad_times = utils.get_random_interval(
      max_ad_times || 1000,
      0,
      1,
      1
    );
    logger("本轮最多看-" + this.max_ad_times + "-个广告");
    //开始看广告
    var ad_interval = AD_CONST_MIN_INTERVAL + random(0, 5);
    logger("即将开始看广告，预计时间" + ad_interval + "s");
    this.run(ad_interval);
  },
  run(ad_interval) {
    var logger = utils.create_logger("ad run: ");
    logger("下面开始看第-" + (this.has_watch_ad_times + 1) + "-个广告");
    logger("本次广告时长预计-" + ad_interval + "-秒");
    var if_current_ad_is_over_interval = 2; //每隔2s检查当前广告是否已经结束
    var is_over_early = false;
    var count = 0;
    while (ad_interval > if_current_ad_is_over_interval) {
      if (this.if_current_ad_is_over && this.if_current_ad_is_over()) {
        var last_wait_interval = random(1, 2);
        logger("广告已结束,再等待" + last_wait_interval + "s后停止当前广告");
        sleep(last_wait_interval * 1000);
        is_over_early = true; //广告实际比预计的时间提前结束
        break;
      }
      sleep(if_current_ad_is_over_interval * 1000);
      ad_interval = ad_interval - if_current_ad_is_over_interval;
      count = count + 1;
      if (count % 5 == 0) {
        logger("广告时间预计还剩" + ad_interval + "秒");
      }
    }
    if (is_over_early) {
      this.has_watch_ad_times = this.has_watch_ad_times + 1;
      this.go_back();
      return;
    }
    if (ad_interval > 0) {
      logger("广告时间还剩最后的" + ad_interval + "秒");
      sleep(ad_interval * 1000);
      this.has_watch_ad_times = this.has_watch_ad_times + 1;
      this.go_back();
      return;
    }
  },
  go_back() {
    var logger = utils.create_logger("ad go_back: ");
    var index = 0;
    while (index < AD_CONST_MAX_GO_BACK_TIMES) {
      back();
      sleep(utils.get_random_interval(1200, 0, 1000, 1));
      for (var i = 0; i < this.has_next_contents.length; i++) {
        var content = this.has_next_contents[i];
        if (utils.exists_text_starts_with(content)) {
          logger("找到-" + content + "-");
          if (this.has_watch_ad_times >= this.max_ad_times) {
            var leave_contents = ["残忍离开"];
            for (var i = 0; i < leave_contents.length; i++) {
              var leave_content = leave_contents[i];
              if (utils.click_text_starts_with(leave_content)) {
                logger(
                  "本轮看广告次数大于-" +
                    this.max_ad_times +
                    "-，找到-" +
                    leave_content +
                    "-,将结束本轮看广告"
                );
                sleep(utils.get_random_interval(1200, 0, 1000, 1));
                this.in_task = true;
                this.after();
                return;
              }
            }
          }

          utils.click_text_starts_with(content);
          sleep(2 * 1000);
          //开始看下一次的广告
          var ad_interval = AD_CONST_MIN_INTERVAL + random(0, 5);
          logger("即将开始看广告，预计时间" + ad_interval + "s");
          this.run(ad_interval);
          return;
        }
      }
      if (this.if_in_task && this.if_in_task()) {
        logger("判断在任务页");
        this.in_task = true;
        this.after();
        return;
      }
      if (this.if_in_home && this.if_in_home()) {
        logger("判断在app首页");
        this.in_home = true;
        this.after();
        return;
      }
      console.log("ad go_back: 第" + (index + 1) + "次执行back");
      index = index + 1;
    }
    this.after();
  },
  after() {
    console.log("ad after");
    this.next();
  },
  next() {
    console.log("ad next");
    console.hide();
  },
};
module.exports = ad;
